package com.ganmiao.server.sys.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ganmiao.server.sys.entity.SysRoleMenu;
import com.ganmiao.server.sys.mapper.SysRoleMenuMapper;
import com.ganmiao.server.sys.repository.SysRoleMenuRepository;
import com.ganmiao.utils.StatusUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 角色菜单信息
 */
@Service
public class SysRoleMenuService {

    //角色菜单信息
    @Autowired
    private SysRoleMenuRepository sysRoleMenuRepository;

    //角色菜单信息
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    /**
     * 新增角色菜单信息
     *
     * @param roleId
     * @param menuIds
     * @return
     */
    public boolean addSysRoleMenu(Integer roleId, Map<String, List<Integer>> menuIds) {

        //角色菜单信息数据
        List<SysRoleMenu> list = new ArrayList<>();

        //角色菜单信息数据赋值
        menuIds.forEach((key, value) -> {
            System.out.println("key:" + key);
            for (Integer i : value) {
                System.out.println("value:" + i);
                SysRoleMenu sysRoleMenu = SysRoleMenu.builder()
                        .roleId(roleId)
                        .menuId(i)
                        .menuList(key)
                        .build();
                list.add(sysRoleMenu);
            }
        });

        if (list.isEmpty()) {
            return true;
        }

        return sysRoleMenuMapper.addSysRoleMenu(list);

    }

    /**
     * 删除角色菜单 (单个菜单)
     *
     * @param menuId
     * @return
     */
    public boolean deleteSysRoleMenuByMenuId(Integer menuId) {

        //删除条件
        LambdaUpdateWrapper<SysRoleMenu> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysRoleMenu::getMenuId, menuId)
                .set(SysRoleMenu::getStatus, StatusUtils.FAILURE);
        return sysRoleMenuRepository.update(updateWrapper);

    }

    /**
     * 删除角色菜单 (角色所有菜单)
     *
     * @param roleId
     * @return
     */
    public boolean deleteSysRoleMenuByRoleId(Integer roleId) {

        //删除条件
        LambdaUpdateWrapper<SysRoleMenu> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysRoleMenu::getRoleId, roleId)
                .set(SysRoleMenu::getStatus, StatusUtils.FAILURE);
        return sysRoleMenuRepository.update(updateWrapper);

    }

    /**
     * 修改角色菜单信息
     *
     * @param roleId
     * @param menuIds
     * @return
     */
    public boolean updateSysRoleMenu(Integer roleId, Map<String, List<Integer>> menuIds) {

        //删除角色菜单信息
        boolean deletedSysRoleMenuByRoleId = deleteSysRoleMenuByRoleId(roleId);

        //新增角色菜单信息
        addSysRoleMenu(roleId, menuIds);

        return deletedSysRoleMenuByRoleId;

    }

    /**
     * 获取菜单ID集合
     *
     * @param roleId
     * @return
     */
    public List<Integer> getMenuIds(Integer roleId, String menuList) {

        //查询条件
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleMenu::getRoleId, roleId)
                .eq(SysRoleMenu::getMenuList, menuList)
                .eq(SysRoleMenu::getStatus, StatusUtils.PASS);
        List<SysRoleMenu> list = sysRoleMenuRepository.list(queryWrapper);

        return list.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());

    }

}
